package org.paradise.controller;
import com.alibaba.fastjson.JSON;
import org.apache.ibatis.annotations.Param;
import org.paradise.dao.AdminDao;
import org.paradise.pojo.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/Login")
public class LoginController {
    @Autowired
    private AdminDao adminDao;
    //跳转登录页面
    @RequestMapping("/login")
    public String main()throws Exception{
        return "/ParadiseSystem/Login/login";
    }
    //登陆成功
    @RequestMapping("/success")
    @ResponseBody
    public Map<String,Object> success(String account, String password, HttpSession session) throws Exception{
        Map<String,Object> map=new HashMap<>();
        Admin admin=adminDao.getlogin(account,password);
        if (admin==null){
            map.put("code",1);
            return map;
        }
        if(admin != null && admin.getState() == 1){
            map.put("code",2);
            return map;
        }
        session.setAttribute("loginout",admin.getName());
        session.setAttribute("admin", JSON.toJSON(admin));
        map.put("code",0);
        return map;
    }

    //跳转登录页面
    @RequestMapping("/loginout")
    public String loginout(HttpServletRequest req, HttpServletResponse resp)throws Exception{
        req.removeAttribute("loginout");
        return "/ParadiseSystem/Login/login";
    }
    //修改密码
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> update(Admin admin) throws Exception{
        Map<String,Object> map = new HashMap<>();
        admin.getId();
        if(admin.getState() == null){
            admin.setState(0);
        }
        adminDao.update(admin);
        map.put("result",true);
        return map;
    }

}